var app=getApp();
var ux = require('../../../../lib/ux.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {   
    province: {},
    city: {},
    area: {},
    country: {},
    list:[],
    animationAddressMenu: {},
    addressMenuIsShow: false,
    thisType: 0,
    pidUrl: "/api/address/pidAdderssList.do", 
	action:"",
	submit:"{{submit}}"
  },
  deletThis:function(){
    this.setData({
      province: {},
      city: {},
      area: {},
      country: {},
      list: [],
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
	  if(this.data.action){
		  var $this = this;
		  ux.request(this.data.action,options,function(protocol){
				$this.setData(protocol.data);
		  });
	  }    
    // 初始化动画变量
    var animation = wx.createAnimation({
      duration: 800,
      transformOrigin: "50% 50%",
      timingFunction: 'ease',
    })
    this.animation = animation;
  },
  
  onForm:function(event){
	if(this.data.submit){
		  var $this = this;
		  var dataset = event.currentTarget.dataset;
		  ux.request(this.data.submit,event.detail.value,function(protocol){
				if(protocol.success){
					if(dataset.url){
						wx.redirectTo({url:dataset.url});
					}
				}
		  },true);
	 }
  },
  
  onTap:function(event){
	  var dataset = event.currentTarget.dataset;
	  var data = this.data;
	  if(dataset.key && dataset.val){
		  data[dataset.key] = dataset.val;
	  }
	  this.setData(data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },


  // 点击所在地区弹出选择框
  selectDistrict: function (e) {
    var that = this
    // 如果已经显示，不在执行显示动画
    if (that.data.addressMenuIsShow) {
      return
    }
    // 执行显示动画
    that.startAddressAnimation(true)
  },
  // 执行动画
  startAddressAnimation: function (isShow) {
    console.log(isShow)
    var that = this
    if (isShow) {
      // vh是用来表示尺寸的单位，高度全屏是100vh
      that.animation.translateY(0 + 'vh').step()
    } else {
      that.animation.translateY(100 + 'vh').step()
    }
    that.setData({
      animationAddressMenu: that.animation.export(),
      addressMenuIsShow: isShow,
    })
  },
  getProvince: function () {
    var $this = this;
    var params = { pid: 0 }
    ux.request(this.data.pidUrl, params, function (protocol) {
      $this.setData({
        list: protocol.data.list,
        thisType: 1,
        province: {},
        city: {},
        area: {},
        country: {}
      });
    }, true);
  },
  getCity: function () {
    var $this = this;
    var params = { pid: this.data.province.id }
    ux.request(this.data.pidUrl, params, function (protocol) {
      $this.setData({
        list: protocol.data.list,
        thisType: 2,
        city: {},
        area: {},
        country: {}
      });
    }, true);
  },
  getArea: function () {
    var $this = this;
    var params = { pid: this.data.city.id }
    ux.request(this.data.pidUrl, params, function (protocol) {
      $this.setData({
        list: protocol.data.list,
        thisType: 3,
        area: {},
        country: {}
      });
    }, true);
  },
  getCountry: function () {
    var $this = this;
    var params = { pid: this.data.area.id }
    ux.request(this.data.pidUrl, params, function (protocol) {
      $this.setData({
        list: protocol.data.list,
        thisType: 4,
        country: {}
      });
    }, true);
  },
  selectThis: function (event) {
    var dataset = event.currentTarget.dataset;
    var lists = this.data.list;
    var index = dataset.index;
    if (this.data.thisType == 1) {
      this.setData({
        province: lists[index],
        thisColor: 1
      })
      this.getCity();
    } else if (this.data.thisType == 2) {
      this.setData({
        city: lists[index],
        thisColor: 2
      })
      this.getArea();
    } else if (this.data.thisType == 3) {
      this.setData({
        area: lists[index],
        thisColor: 3
      })
      this.getCountry();
    } else if (this.data.thisType == 4) {
      this.setData({
        country: lists[index],
        thisColor: 4
      })
    }

  },
  citySure: function () {
    var that = this
    that.startAddressAnimation(false)
  },
  toast: function (params) {
    if (typeof (params) == "string") {
      params = { content: params };
    }
    this.setData({
      toast: params.content
    });
    var THIS = this;
    setTimeout(function () {
      THIS.setData({
        toast: false
      });
      if (params.complete) {
        params.complete();
      }
    }, params.time || 3000);
  },
})
